Imports EntityLayer
Imports BusinessLayer
Imports System.Data


Partial Class CartaPresentacion
    Inherits System.Web.UI.Page

    Dim myDt As DataTable

#Region " Crear - Llenar Datatable "

    Private Function CreateDataTable() As DataTable
        Dim myDataTable As DataTable = New DataTable()

        Dim myDataColumn As DataColumn

        myDataColumn = New DataColumn()
        myDataColumn.DataType = Type.GetType("System.Int32")
        myDataColumn.ColumnName = "Codigo"
        myDataTable.Columns.Add(myDataColumn)

        myDataColumn = New DataColumn()
        myDataColumn.DataType = Type.GetType("System.String")
        myDataColumn.ColumnName = "Producto"
        myDataTable.Columns.Add(myDataColumn)

        myDataColumn = New DataColumn()
        myDataColumn.DataType = Type.GetType("System.String")
        myDataColumn.ColumnName = "UnidadMedida"
        myDataTable.Columns.Add(myDataColumn)

        myDataColumn = New DataColumn()
        myDataColumn.DataType = Type.GetType("System.String")
        myDataColumn.ColumnName = "PrecioUnitario"
        myDataTable.Columns.Add(myDataColumn)

        Return myDataTable
    End Function

    Private Function AddDataToTable(ByVal intCodigo As Integer, ByVal strProducto As String, ByVal strUnidadMedida As String, ByVal strPrecioUnitario As String, ByVal myTable As DataTable)
        Dim row As DataRow

        row = myTable.NewRow()

        row("Codigo") = intCodigo
        row("Producto") = strProducto
        row("UnidadMedida") = strUnidadMedida
        row("PrecioUnitario") = strPrecioUnitario

        myTable.Rows.Add(row)
    End Function

#End Region

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            Dim strIdCarta As String
            strIdCarta = Request.QueryString("NC")

            If strIdCarta = Nothing Then
                hfAccion.Value = "ADD"

                Call GenerarCodigos()

                txtFechaEmision.Text = Now.Date.ToShortDateString

                myDt = New DataTable()
                myDt = CreateDataTable()
                Session("myDatatable") = myDt

                gvwProducto.DataSource = (CType(Session("myDatatable"), DataTable)).DefaultView
                gvwProducto.DataBind()
            Else
                hfAccion.Value = "UDP"

                Dim dt As DataTable
                dt = CartaPresentacionBL.ListarCartaPresentacionxCodigo(strIdCarta).Tables(0)

                txtCodCarta.Text = dt.Rows(0)(0).ToString
                txtFechaEmision.Text = dt.Rows(0)(1).ToString
                txtDestinatario.Text = dt.Rows(0)(2).ToString
                txtDescripcion.Text = dt.Rows(0)(3).ToString
                txtPostulante.Text = dt.Rows(0)(4).ToString
                txtRUC.Text = dt.Rows(0)(5).ToString
                txtRazonSocial.Text = dt.Rows(0)(6).ToString
                txtDireccion.Text = dt.Rows(0)(7).ToString
                txtRubro.Text = dt.Rows(0)(8).ToString
                txtContacto.Text = dt.Rows(0)(9).ToString
                txtTelefono.Text = dt.Rows(0)(10).ToString
                txtEmail.Text = dt.Rows(0)(11).ToString
                txtUrl.Text = dt.Rows(0)(12).ToString

                Session("myDatatable") = ProductoPostulanteBL.ListarProductosAsignados(strIdCarta).Tables(0)

                gvwProducto.DataSource = CType(Session("myDatatable"), DataTable).DefaultView
                gvwProducto.DataBind()

            End If

        End If
    End Sub

    Private Sub GenerarCodigos()
        txtCodCarta.Text = CartaPresentacionBL.ObtenerNuevoCodigo
        txtPostulante.Text = ProveedorBL.ObtenerNuevoCodigo
    End Sub

    Protected Sub AgregarProducto_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnAgregarProducto.Click
        mpeProducto.Show()
        gvwListaProducto.DataSource = ProductoBL.ListarProductos
        gvwListaProducto.DataBind()
    End Sub

    Protected Sub gvwListaProducto_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles gvwListaProducto.SelectedIndexChanged

        With ProductoBL.ListarProductos.Item(gvwListaProducto.SelectedIndex)

            AddDataToTable(.Codigo.ToString(), .Nombre.ToString, .UnidadMedida.ToString, .PrecioUnitario.ToString, CType(Session("myDatatable"), DataTable))

            Call MostrarProductoSeleccionado()

        End With

    End Sub

    Private Sub MostrarProductoSeleccionado()
        gvwProducto.DataSource = CType(Session("myDatatable"), DataTable).DefaultView
        gvwProducto.DataBind()
    End Sub

    Protected Sub gvwProducto_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles gvwProducto.RowDeleting
        Dim dt As DataTable = CType(Session("myDatatable"), DataTable)
        dt.Rows(e.RowIndex).Delete()

        If hfAccion.Value = "UDP" Then
            ProductoPostulanteBL.Eliminar(txtCodCarta.Text, gvwProducto.Rows(e.RowIndex).Cells(0).Text)
        End If

        Session("myDatatable") = dt

        Call MostrarProductoSeleccionado()
    End Sub

    Protected Sub btnGrabar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnGrabar.Click

        If hfAccion.Value = "ADD" Then

            If gvwProducto.Rows.Count > 0 Then

                Dim rsPostulante As New Proveedor
                rsPostulante.Codigo = txtPostulante.Text.Trim
                rsPostulante.RUC = txtRUC.Text.Trim
                rsPostulante.RazonSocial = txtRazonSocial.Text
                rsPostulante.Direccion = txtDireccion.Text
                rsPostulante.Rubro = txtRubro.Text
                rsPostulante.Contacto = txtContacto.Text
                rsPostulante.Telefono = txtTelefono.Text
                rsPostulante.Email = txtEmail.Text

                If String.IsNullOrEmpty(txtUrl.Text) Then
                    rsPostulante.Url = ""
                Else
                    rsPostulante.Url = txtUrl.Text
                End If

                If ProveedorBL.Agregar(rsPostulante) = True Then
                    Dim rsCarta As New CartaPresentacionE
                    rsCarta.NumeroCarta = txtCodCarta.Text
                    rsCarta.FechaEmision = CDate(txtFechaEmision.Text.Trim)
                    rsCarta.Destinatario = txtDestinatario.Text.Trim
                    rsCarta.Descripcion = txtDescripcion.Text.Trim
                    rsCarta.Aprobacion = 0
                    rsCarta.Codigo = txtPostulante.Text.Trim

                    If CartaPresentacionBL.Agregar(rsCarta) = True Then

                        For Each Fila As GridViewRow In gvwProducto.Rows
                            Dim rsProducto As New ProductoPostulante
                            rsProducto.CodigoProducto = Fila.Cells(0).Text
                            rsProducto.NombreProducto = Fila.Cells(1).Text
                            rsProducto.NumeroCarta = txtCodCarta.Text

                            ProductoPostulanteBL.Agregar(rsProducto)
                        Next

                    End If

                    Call GenerarCodigos()
                    Call Limpiar()
                End If
            End If
        ElseIf hfAccion.Value = "UDP" Then
            If gvwProducto.Rows.Count > 0 Then

                Dim rsPostulante As New Proveedor
                rsPostulante.Codigo = txtPostulante.Text.Trim
                rsPostulante.RUC = txtRUC.Text.Trim
                rsPostulante.RazonSocial = txtRazonSocial.Text
                rsPostulante.Direccion = txtDireccion.Text
                rsPostulante.Rubro = txtRubro.Text
                rsPostulante.Contacto = txtContacto.Text
                rsPostulante.Telefono = txtTelefono.Text
                rsPostulante.Email = txtEmail.Text
                If String.IsNullOrEmpty(txtUrl.Text) Then
                    rsPostulante.Url = ""
                Else
                    rsPostulante.Url = txtUrl.Text
                End If

                If ProveedorBL.Actualizar(rsPostulante) = True Then
                    Dim rsCarta As New CartaPresentacionE
                    rsCarta.NumeroCarta = txtCodCarta.Text
                    rsCarta.FechaEmision = CDate(txtFechaEmision.Text.Trim)
                    rsCarta.Destinatario = txtDestinatario.Text.Trim
                    rsCarta.Descripcion = txtDescripcion.Text.Trim
                    rsCarta.Aprobacion = 0
                    rsCarta.Codigo = txtPostulante.Text.Trim

                    If CartaPresentacionBL.Actualizar(rsCarta) = True Then

                        For Each Fila As GridViewRow In gvwProducto.Rows
                            Dim rsProducto As New ProductoPostulante
                            rsProducto.CodigoProducto = Fila.Cells(0).Text
                            rsProducto.NombreProducto = Fila.Cells(1).Text
                            rsProducto.NumeroCarta = txtCodCarta.Text

                            ProductoPostulanteBL.Agregar(rsProducto)
                        Next

                    End If

                    Response.Redirect("ListaCartaPresentacion.aspx")
                End If
            End If
            End If

    End Sub

    Private Sub Limpiar()
        txtRUC.Text = String.Empty
        txtRazonSocial.Text = String.Empty
        txtDestinatario.Text = String.Empty
        txtDescripcion.Text = String.Empty
        txtDireccion.Text = String.Empty
        txtRubro.Text = String.Empty
        txtContacto.Text = String.Empty
        txtTelefono.Text = String.Empty
        txtEmail.Text = String.Empty
        txtUrl.Text = String.Empty

        Session("myDatatable") = Nothing

        gvwProducto.DataSource = Nothing
        gvwProducto.DataBind()
    End Sub

    Protected Sub btnCancelar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        Call Limpiar()

        Response.Redirect("ListaCartaPresentacion.aspx")
    End Sub
End Class
